Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
koa-better-error-handler
Advanced tools
A better error-handler for Lad and Koa. Makes `ctx.throw` awesome (best used with koa-404-handler)
A better error-handler for Lad and Koa. Makes
ctx.throw
awesome (best used with koa-404-handler)
<ul>
for Mongoose validation errors with more than one messagectx.throw
beautiful messages (e.g. ctx.throw(404)
will output a beautiful error object :hibiscus:)text/html
, application/json
, and text
response typesnpm install --save koa-better-error-handler
You should probably be using this in combination with koa-404-handler too!
No support for sessions, cookies, or flash messaging:
const errorHandler = require('koa-better-error-handler');
const Koa = require('koa');
const Router = require('koa-router');
const koa404Handler = require('koa-404-handler');
// initialize our app
const app = new Koa();
// override koa's undocumented error handler
app.context.onerror = errorHandler;
// specify that this is our api
app.context.api = true;
// use koa-404-handler
app.use(koa404Handler);
// set up some routes
const router = new Router();
// throw an error anywhere you want!
router.get('/404', ctx => ctx.throw(404));
router.get('/500', ctx => ctx.throw(500));
// initialize routes on the app
app.use(router.routes());
// start the server
app.listen(3000);
console.log('listening on port 3000');
Built-in support for sessions, cookies, and flash messaging:
const errorHandler = require('koa-better-error-handler');
const Koa = require('koa');
const redis = require('redis');
const RedisStore = require('koa-redis');
const session = require('koa-generic-session');
const flash = require('koa-connect-flash');
const convert = require('koa-convert');
const Router = require('koa-router');
const koa404Handler = require('koa-404-handler');
// initialize our app
const app = new Koa();
// define keys used for signing cookies
app.keys = ['foo', 'bar'];
// initialize redis store
const redisClient = redis.createClient();
redisClient.on('connect', () => app.emit('log', 'info', 'redis connected'));
redisClient.on('error', err => app.emit('error', err));
// define our storage
const redisStore = new RedisStore({
client: redisClient
});
// add sessions to our app
app.use(
convert(
session({
store: redisStore
})
)
);
// add support for flash messages (e.g. `req.flash('error', 'Oops!')`)
app.use(convert(flash()));
// override koa's undocumented error handler
app.context.onerror = errorHandler;
// use koa-404-handler
app.use(koa404Handler);
// set up some routes
const router = new Router();
// throw an error anywhere you want!
router.get('/404', ctx => ctx.throw(404));
router.get('/500', ctx => ctx.throw(500));
// initialize routes on the app
app.use(router.routes());
// start the server
app.listen(3000);
console.log('listening on port 3000');
Example Request:
curl -H "Accept: application/json" http://localhost/some-page-does-not-exist
Example Response:
{
"statusCode": 404,
"error": "Not Found",
"message":"Not Found"
}
If you specify app.context.api = true
or set ctx.api = true
, and if a Mongoose validation error message occurs that has more than one message (e.g. multiple fields were invalid) – then err.message
will be joined by a comma instead of by <li>
.
Therefore if you DO want your API error messages to return HTML formatted error lists for Mongoose validation, then set app.context.api = false
, ctx.api = false
, or simply make sure to not set them before using this error handler.
try {
// trigger manual validation
// (this allows us to have a 400 error code instead of 500)
await company.validate();
} catch (err) {
ctx.throw(Boom.badRequest(err));
}
With error lists:
{
"statusCode": 400,
"error": "Bad Request",
"message": "<ul class=\"text-xs-left mb-0\"><li>Path `company_logo` is required.</li><li>Gig description must be 100-300 characters.</li></ul>"
}
Without error lists:
{
"statusCode":400,
"error":"Bad Request",
"message":"Path `company_logo` is required., Gig description must be 100-300 characters."
}
MIT © Nick Baugh
FAQs
A better error-handler for Lad and Koa. Makes `ctx.throw` awesome (best used with koa-404-handler)
The npm package koa-better-error-handler receives a total of 2,587 weekly downloads. As such, koa-better-error-handler popularity was classified as popular.
We found that koa-better-error-handler demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.